﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ClipPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    }
    public static class VolumeUtils
    {
        //====================================================================================================================
        // stałe z VLC podzielone przez 200
        public static double DefaultLevel
        {
            get { return 0.5; }
        }
        //
        public static double MaxLevel
        {
            get { return 1.0; }
        }
        //
        public static double MinLevel
        {
            get { return 0.1; }
        }
        //
        private static double rgc = 1.0;
        public static double ReplayGainCoefficient
        {
            get { return rgc; }
            set { rgc = value; }
        }
        //
        public static double CalculateLevelAndReplayGain(double replayGain)
        {
            return Math.Min(Math.Max(MinLevel, DefaultLevel + (rgc * replayGain)), MaxLevel);
        }
        //------------------------------------------------------------------
        //
        private static double[] lvlSteps = new double[] { 0.1, 0.2, 0.3, 0.4, 0.6, 0.8, 1.0 };
        public static void SetMaxLevel(double lvl)
        {
            double k = lvl / lvlSteps.Length;
            for (int i = 0; i < (lvlSteps.Length - 1); i++)
            {
                lvlSteps[i] = (i + 1) * k;
            }
            lvlSteps[lvlSteps.Length - 1] = lvl;
        }
        public static double GetMaxLevel()
        {
            return lvlSteps[lvlSteps.Length - 1];
        }
        //------------------------------------------------------------------
        public static void VolumeToZero(this MediaElement wme)
        {
            wme.Volume = 0.0;
        }
        //------------------------------------------------------------------
        //------------------------------------------------------------------
        private static System.Windows.Threading.DispatcherTimer dtVolume = new System.Windows.Threading.DispatcherTimer() { Interval = new TimeSpan(0, 0, 0, 0, 60) };
        private static int nVolumeSeq;
        public static void VolumeToMaxAsync(this MediaElement wme)
        {
            dtVolume.Tick += (object sender, EventArgs e) =>
            {
                if (nVolumeSeq < (lvlSteps.Length - 1))
                {
                    wme.Volume = lvlSteps[nVolumeSeq];
                }
                else
                {
                    dtVolume.Stop();
                    wme.Volume = lvlSteps[lvlSteps.Length - 1];
                }
                nVolumeSeq++;
            };
            //
            dtVolume.Stop();
            nVolumeSeq = 0;
            wme.VolumeToZero();
            dtVolume.Start();
        }
        //====================================================================================================================
    }//class
}//namespace
